package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.getRFC;

import java.io.File;
import java.io.FileWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;




public class GenerateXML {

	public void exportXML(String[] ids, String path){
		for(int i =0;i> ids.length;  i ++ ){
			try {
				exportXML(ids[i], path);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		
		
	}
	
	public void fullExport(String path) throws SQLException{
		String query;
		DatabaseConnection db = new DatabaseConnection();
		query = "SELECT * FROM `CMS_RFC`";
		ResultSet rs = db.query(query);
		while(rs.next()){
			exportXML(rs.getString(RFC.TC_ID), path);
		}

		
		
	}
	public void exportXML(RFC rfc, String path) throws SQLException{
		exportXML(rfc.getIdRFC(), path);
	}
	
	public void exportXML(String id, String path) throws SQLException{
		//get ResulteSet of the RFC from the Database:
		String query;
		DatabaseConnection db = new DatabaseConnection();
		query = "SELECT * FROM `CMS_RFC` where "+ RFC.TC_ID + "='" + id + "'"; 
		ResultSet rs = db.query(query);
		rs.next();
		//Generate the XML File
		
	
		File exportPath = new File(path);
		
		
		Element xmlRFC = new Element("RFC");
		Document xmlDocument = new Document(xmlRFC);
		xmlRFC.setAttribute("idRFC", rs.getString(RFC.TC_ID));
		xmlRFC.addContent(new Element(RFC.TC_AFFECTEDINFRASTRUCTURE).addContent(rs.getString(RFC.TC_AFFECTEDINFRASTRUCTURE)));
		xmlRFC.addContent(new Element(RFC.TC_AFFECTEDSERVICES).addContent(rs.getString(RFC.TC_AFFECTEDSERVICES)));
		xmlRFC.addContent(new Element(RFC.TC_APPROVALPERSON).addContent(rs.getString(RFC.TC_APPROVALPERSON)));
		xmlRFC.addContent(new Element(RFC.TC_BENEFITS).addContent(rs.getString(RFC.TC_BENEFITS)));
		xmlRFC.addContent(new Element(RFC.TC_CHANGEOWNER).addContent(rs.getString(RFC.TC_CHANGEOWNER)));
		xmlRFC.addContent(new Element(RFC.TC_COSTS).addContent(rs.getString(RFC.TC_COSTS)));
		xmlRFC.addContent(new Element(RFC.TC_DATEOFREJECTIONORAPPROVAL).addContent(rs.getString(RFC.TC_DATEOFREJECTIONORAPPROVAL)));
		xmlRFC.addContent(new Element(RFC.TC_PRIORITY).addContent(rs.getString(RFC.TC_PRIORITY)));
		xmlRFC.addContent(new Element(RFC.TC_REASONFORCHANGE).addContent(rs.getString(RFC.TC_REASONFORCHANGE)));
		xmlRFC.addContent(new Element(RFC.TC_REASONOFREJECTING).addContent(rs.getString(RFC.TC_REASONOFREJECTING)));
		xmlRFC.addContent(new Element(RFC.TC_RFCINITIATOR).addContent(rs.getString(RFC.TC_RFCINITIATOR)));
		xmlRFC.addContent(new Element(RFC.TC_RISKS).addContent(rs.getString(RFC.TC_RISKS)));
		xmlRFC.addContent(new Element(RFC.TC_Status).addContent(rs.getString(RFC.TC_Status)));
		xmlRFC.addContent(new Element(RFC.TC_SUBMISSIONDATE).addContent(rs.getString(RFC.TC_SUBMISSIONDATE)));
		xmlRFC.addContent(new Element(RFC.TC_SUMMARY).addContent(rs.getString(RFC.TC_SUMMARY)));
		
		//Write the XML to the specified location
	

		try {
			while(!exportPath.exists()){
				
				exportPath.mkdirs();
				
			}
			
			XMLOutputter outputter = new XMLOutputter();
			FileWriter writer = new FileWriter(path + rs.getString(RFC.TC_ID) + ".xml");
			outputter.output(xmlDocument, writer);
			writer.close();
			} catch (java.io.IOException e) {
				e.printStackTrace();
				}
		
		
	}
	
	
}
